package aom.ju.ss.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


import aom.ju.ss.mode.LpKey;
import aom.ju.ss.utils.LogUtils;
import aom.ju.ss.utils.NewToastUtilsKt;
import aom.ju.ss.utils.SendMsg;

import static com.tencent.mm.opensdk.constants.ConstantsAPI.COMMAND_SENDAUTH;
import static com.tencent.mm.opensdk.constants.ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, LpKey.INSTANCE.getWX_APP_ID());
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception ignored) {
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        try {
            setIntent(intent);
            api.handleIntent(intent, this);
        } catch (Exception e) {
        }
    }

    @Override
    public void onReq(BaseReq req) {

        LogUtils.e("12121212121212wx" + req.getType());
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onResp(BaseResp resp) {

        LogUtils.e("12121212121212wxonResp" + resp.getType());
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            LogUtils.e("f (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX");
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    NewToastUtilsKt.show("支付成功");
                    //微信支付成功
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    NewToastUtilsKt.show("您已取消了本次订单的支付");
                    //微信支付失败
                    break;
                default:
                    //微信支付失败
                    NewToastUtilsKt.show("支付失败");
                    break;

            }
            this.finish();
        } else if (resp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            WXLaunchMiniProgram.Resp resps = (WXLaunchMiniProgram.Resp) resp;
            LogUtils.e("wxonResp" + resps.extMsg);
            if (resps.extMsg.equals("true")) {
                NewToastUtilsKt.show("支付成功");
                //微信支付成功
            } else {
                NewToastUtilsKt.show("支付失败");
            }
            finish();
        } else if (resp.getType() == COMMAND_SENDMESSAGE_TO_WX) {
            finish();
        } else if (resp.getType() == COMMAND_SENDAUTH) { //登录授权

            SendAuth.Resp resp1 = (SendAuth.Resp) resp;
            switch (resp.errCode) {
                case 0://同意
                    SendMsg.Companion.sendEvent(LpKey.INSTANCE.getWX_LOGIN_OK(), resp1.code);
                    break;
                case -4://拒绝
                    NewToastUtilsKt.show("拒绝授权");
                    break;
                case -2://取消
                    NewToastUtilsKt.show("取消授权");
                    break;
            }
            finish();

        }
    }
}